package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * This entity represents kind of training.
 * Examples: Linux server administration course; First aid course, etc.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="TRAINING_TYPE")
@NamedQueries({
	@NamedQuery(name="findTrainingTypeByCode", query="SELECT tt FROM TrainingTypeEntityImpl tt WHERE tt.code = :code")
})
public class TrainingTypeEntityImpl extends CommonPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = 4358442447516594030L;
	
	/*
	 * Entity attributes (some inherited).
	 */
  private long defaultPeriodInSeconds;

	@Column(nullable=true)
	private String evidencePassName;

	/*
	 * Relations
	 */
	@OneToMany(fetch=FetchType.EAGER, targetEntity=TrainingEntityImpl.class, mappedBy="trainingType", cascade={CascadeType.REMOVE})
	private Set<TrainingEntityImpl> trainings;

	@OneToMany(fetch=FetchType.EAGER, targetEntity=TrainingTypeForRoleTypeEntityImpl.class, mappedBy="trainingType", cascade={CascadeType.REMOVE})
	private Set<TrainingTypeForRoleTypeEntityImpl> trainingTypeForRoleTypes;

	public TrainingTypeEntityImpl()
	{
		super(null, null, null);
	}

	public TrainingTypeEntityImpl(String trainingCode, String trainingName, String trainingDescription, String evidencePassName, long defaultPeriodInSeconds)
	{
		super(trainingCode, trainingName, trainingDescription);
		this.evidencePassName = evidencePassName;
		this.defaultPeriodInSeconds = defaultPeriodInSeconds;
	}

	public Set<TrainingEntityImpl> getTrainings()
	{
		return trainings;
	}

	public List<TrainingEntityImpl> getTrainingsAsList()
	{
		return new ArrayList<TrainingEntityImpl>(getTrainings());
	}

	public Set<TrainingTypeForRoleTypeEntityImpl> getTrainingTypeForRoleTypes()
	{
		return trainingTypeForRoleTypes;
	}

	public List<TrainingTypeForRoleTypeEntityImpl> getTrainingTypeForRoleTypesAsList()
	{
		return new ArrayList<TrainingTypeForRoleTypeEntityImpl>(getTrainingTypeForRoleTypes());
	}

	public long getDefaultPeriodInSeconds()
	{
		return defaultPeriodInSeconds;
	}

	public void setDefaultPeriodInSeconds(long defaultPeriodInSeconds)
	{
		this.defaultPeriodInSeconds = defaultPeriodInSeconds;
	}

	public String getEvidencePassName()
	{
		return evidencePassName;
	}

	public void setEvidencePassName(String evidencePassName)
	{
		this.evidencePassName = evidencePassName;
	}

	
  public void setTrainings(Set<TrainingEntityImpl> trainings)
  {
    this.trainings = trainings;
  }

  
  public void setTrainingTypeForRoleTypes(Set<TrainingTypeForRoleTypeEntityImpl> trainingTypeForRoleTypes)
  {
    this.trainingTypeForRoleTypes = trainingTypeForRoleTypes;
  }

  @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("TrainingTypeEntityImpl [defaultPeriodInSeconds=");
		builder.append(defaultPeriodInSeconds);
		builder.append(", evidencePassName=");
		builder.append(evidencePassName);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.TrainingType#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getTrainingTypeForRoleTypes().size() == 0 && getTrainings().size() == 0;
	}
}
